﻿//////////////////////////////////////////////////////////////////////////
// FileName:fmCarBreakRuleManager.aspx
// CreateBy:DragonXiong
// CreateDate:2010-05-25
// Description:
//		Car BreakRule Manager
//      
// Modify Log:
//	
// Copyright:
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Web.Common;
using Auto.Components;
using Model = Auto.Data.BusMan.Model;
using BLL = Auto.Data.BusMan.BLL;

namespace Auto.BusMan
{
    public partial class fmCarBreakRuleManager : System.Web.UI.Page
    {
        public string DocID
        {
            get
            {
                ViewState["DocID"] = Request.QueryString["DocID"];
                return (ViewState["DocID"] == null) ? "" : ViewState["DocID"].ToString();
            }
            set { ViewState["DocID"] = value; }
        }

        Model.BusBreakRules model = new Model.BusBreakRules();
        BLL.BusBreakRules bll = new BLL.BusBreakRules();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            if (string.IsNullOrEmpty(DocID)) //新增
            {
                this.chkAddContinue.Checked = true;
                checktr.Visible = true;
                checktr.Attributes.Add("style", "display:block;");
                InitControl();
            }
            else  //修改
            {
                checktr.Attributes.Add("style", "display:none;");
                BindData();
            }
        }

        private void InitControl()
        {
            this.UcDDLCarSelector1.InitControl();
            this.UcDDLKeepDiv1.InitControl();
            this.UcDriverDDLSelector1.InitControl();
            UcBreakRuleTypePhraseDropDownList.InitControl();
        }

        private void ClearText()
        {
            this.UcDDLCarSelector1.SelectedValue = "";// this.txtBusIdentificationNO.Text = "";//临时//this.txtBusID.Text = model.BusID;
            //this.txtCompanyID.Text = model.CompanyID;
            this.UcDDLKeepDiv1.SelectedValue = "";// this.txtCompanyName.Text = "";// model.CompanyName;
            //this.txtDriverID.Text = model.DriverID;
            this.UcDriverDDLSelector1.SelectedValue = "";// this.txtDriver.Text = "";// model.DriverName;
            this.txtBreakRuleTime.Text = "";// model.BreakRuleTime.ToString();
            this.UcBreakRuleTypePhraseDropDownList.SelectedValue = "";// this.txtBreakRuleType.Text = "";//model.BreakRuleType;
            this.txtBreakRuleLocation.Text = "";// model.BreakRuleLocation;
            this.txtBreakRuleNote.Text = "";// model.BreakRuleNote;
            this.txtForfeit.Text = "";// model.Forfeit.ToString();
            //this.txtNote.Text = model.Note;
        }

        private void BindData()
        {
            InitControl();
            model = bll.GetModel(DocID);
            Session["fmCarBreakRuleManager.Model"] = model;
            this.UcDDLCarSelector1.SelectedValue = model.BusID;// this.txtBusIdentificationNO.Text = model.BusID;//临时 //this.txtBusID.Text = model.BusID;
            //this.txtCompanyID.Text = model.CompanyID;
            this.UcDDLKeepDiv1.SelectedValue = model.CompanyID;// this.txtCompanyName.Text = model.CompanyName;
            //this.txtDriverID.Text = model.DriverID;
            this.UcDriverDDLSelector1.SelectedValue = model.DriverID;// this.txtDriver.Text = model.DriverName;
            this.txtBreakRuleTime.CalendarDate = model.BreakRuleTime;
            UcBreakRuleTypePhraseDropDownList.SelectedValue = model.BreakRuleType;// this.txtBreakRuleType.Text = model.BreakRuleType;
            this.txtBreakRuleLocation.Text = model.BreakRuleLocation;
            this.txtBreakRuleNote.Text = HtmlDiscode(model.BreakRuleNote);
            this.txtForfeit.Text = model.Forfeit.ToString();
            //this.txtNote.Text = model.Note;
        }

        private Model.BusBreakRules GetCarBreakRule()
        {
            if (!string.IsNullOrEmpty(DocID))
            {
                model = (Model.BusBreakRules)Session["fmCarBreakRuleManager.Model"];
            }
            //Tab1
            string BusID = this.UcDDLCarSelector1.SelectedValue;// this.txtBusIdentificationNO.Text;// this.txtBusID.Text;
            string CompanyID = this.UcDDLKeepDiv1.SelectedValue;// this.txtCompanyName.Text;// this.txtCompanyID.Text;
            string CompanyName = this.UcDDLKeepDiv1.SelectedText;// this.txtCompanyName.Text;
            string DriverID = this.UcDriverDDLSelector1.SelectedValue;// this.txtDriver.Text;// this.txtDriverID.Text;
            string DriverName = this.UcDriverDDLSelector1.DriverName;// this.txtDriver.Text;// this.txtDriverName.Text;
            DateTime BreakRuleTime = DateTime.Parse(this.txtBreakRuleTime.Text);
            string BreakRuleType = this.UcBreakRuleTypePhraseDropDownList.SelectedValue;// this.txtBreakRuleType.Text;
            string BreakRuleLocation = this.txtBreakRuleLocation.Text;
            string BreakRuleNote = this.txtBreakRuleNote.Text;
            decimal Forfeit = decimal.Parse(this.txtForfeit.Text);
            //string Note = this.txtNote.Text;

            //Tab1
            model.BusID = BusID;
            model.CompanyID = CompanyID;
            model.CompanyName = CompanyName;
            model.DriverID = DriverID;
            model.DriverName = DriverName;
            model.BreakRuleTime = BreakRuleTime;
            model.BreakRuleType = BreakRuleType;
            model.BreakRuleLocation = BreakRuleLocation;
            model.BreakRuleNote = HtmlEncode(BreakRuleNote);
            model.Forfeit = Forfeit;
            //model.Note = Note;


            return model;
        }

        private bool Check()
        {
            string strErr = "";

            if (UcDDLCarSelector1.SelectedValue == "")
            {
                strErr += "请选择车辆!\\n";
            }
            if (UcDDLKeepDiv1.SelectedValue == "")
            {
                strErr += "请选择单位!\\n";
            }
            if (UcDriverDDLSelector1.SelectedValue == "")
            {
                strErr += "请选择驾驶员!\\n";
            }
            if (UcBreakRuleTypePhraseDropDownList.SelectedValue == "")
            {
                strErr += "请选择违章性质!\\n";
            }
            if (strErr != "")
            {
                Jscript.AjaxAlert(this, strErr);
                return false;
            }
            else
            {
                return true;
            }
        }

        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Check())
            {
                return;
            }

            try
            {
                model = GetCarBreakRule();

                model.UserAcct = SessionBox.User.UserID;
                if (string.IsNullOrEmpty(DocID))
                {
                    model.RecDivID = SessionBox.User.DivID;
                    bll.Add(model);
                }
                else
                {
                    bll.Update(model);
                }
                Redirect();

            }
            catch (Exception ex)
            {
                Jscript.AjaxAlert(this, ex.Message);
            }

        }
        private void Redirect()
        {
            if (chkAddContinue.Checked)
            {
                lblMsg.Text = "保存成功，请继续添加！";
                ClearText();
            }
            else
            {
                Server.Transfer("fmCarBreakRuleList.aspx");
            }
        }

        #endregion Event

        /// <summary>
        /// 替换html中的特殊字符
        /// </summary>
        /// <param name="theString">需要进行替换的文本。</param>
        /// <returns>替换完的文本。</returns>
        private string HtmlEncode(string theString)
        {
            theString = theString.Replace(">", "&gt;");
            theString = theString.Replace("<", "&lt;");
            theString = theString.Replace("  ", " &nbsp;");
            theString = theString.Replace("  ", " &nbsp;");
            theString = theString.Replace("\"", "&quot;");
            theString = theString.Replace("\'", "&#39;");
            theString = theString.Replace("\n", "<br/> ");
            return theString;
        }

        /// <summary>
        /// 恢复html中的特殊字符
        /// </summary>
        /// <param name="theString">需要恢复的文本。</param>
        /// <returns>恢复好的文本。</returns>
        private string HtmlDiscode(string theString)
        {
            theString = theString.Replace("&gt;", ">");
            theString = theString.Replace("&lt;", "<");
            theString = theString.Replace("&nbsp;", " ");
            theString = theString.Replace(" &nbsp;", "  ");
            theString = theString.Replace("&quot;", "\"");
            theString = theString.Replace("&#39;", "\'");
            theString = theString.Replace("<br/> ", "\n");
            return theString;
        }


    }
}